<?php


namespace App\Services;

use App\Admin\Actions\Review\PgReview;
use App\Department\Actions\Review\DeptReview;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;

abstract class BaseApplyService
{
    public $repository;

    abstract public function gridColumns(Grid $grid, $role = 0);

    abstract public function gridPublishColumns(Grid $grid, $role = 0);

    abstract public function formFields(Form $form, $role = 0);

    protected function gridActions(Grid $grid, $role = 0, $closure = null)
    {
        $user = Admin::user();
        $grid->actions(function (Grid\Displayers\Actions $actions) use ($user, $role, $closure) {
            $actions->disableView();
            $row = $actions->row;
            if ($closure instanceof \Closure) {
                $closure($actions, $row);
            }
            if ($role == 1) {
                $actions->disableEdit();
                if (!$row->review_status) {
                    $actions->prepend(new PgReview($actions));
                }
            } else {
                if ($user->id != $row->user_id) {
                    $actions->disableDelete();
                    $actions->disableEdit();
                } elseif ($row->dept_review_status == 1 && in_array($row->review_status, [0, 1])) {
                    $actions->disableEdit();
                    $actions->disableDelete();
                }
                if ($role == 2 && !$row->dept_review_status) {
                    $actions->prepend(new DeptReview($actions));
                }
            }
        });
    }


}
